
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.Sys;
using Apps.Models.Sys;
using Apps.IBLL.Sys;
using System.Linq.Expressions;

namespace Apps.BLL.Sys
{
    public partial class SysUserBLL : Virtual_SysUserBLL, ISysUserBLL
    {
        public List<SysUserModel> GetPagerList(ref GridPager pager, Expression<Func<SysUser, bool>> whereLambda)
        {
            IQueryable<SysUser> queryData = m_Rep.GetList(whereLambda);

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
    }
    public class Virtual_SysUserBLL
	{
        [Dependency]
        public ISysUserRepository m_Rep { get; set; }

		public virtual List<SysUserModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<SysUser> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.Id.Contains(queryStr)
								|| a.UserName.Contains(queryStr)
								|| a.Password.Contains(queryStr)
								|| a.TrueName.Contains(queryStr)
								|| a.Card.Contains(queryStr)
								|| a.MobileNumber.Contains(queryStr)
								|| a.PhoneNumber.Contains(queryStr)
								|| a.QQ.Contains(queryStr)
								|| a.EmailAddress.Contains(queryStr)
								|| a.OtherContact.Contains(queryStr)
								|| a.Province.Contains(queryStr)
								|| a.City.Contains(queryStr)
								|| a.Village.Contains(queryStr)
								|| a.Address.Contains(queryStr)
								
								
								|| a.CreatePerson.Contains(queryStr)
								|| a.Sex.Contains(queryStr)
								
								
								|| a.Marital.Contains(queryStr)
								|| a.Political.Contains(queryStr)
								|| a.Nationality.Contains(queryStr)
								|| a.Native.Contains(queryStr)
								|| a.School.Contains(queryStr)
								|| a.Professional.Contains(queryStr)
								|| a.Degree.Contains(queryStr)
								|| a.DepId.Contains(queryStr)
								|| a.PosId.Contains(queryStr)
								|| a.Expertise.Contains(queryStr)
								
								|| a.Photo.Contains(queryStr)
								|| a.Attach.Contains(queryStr)
								|| a.Lead.Contains(queryStr)
								|| a.LeadName.Contains(queryStr)
								
								
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<SysUserModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<SysUserModel>();
		}

        public virtual List<SysUserModel> CreateModelList(ref IQueryable<SysUser> queryData)
        {

            List<SysUserModel> modelList = (from r in queryData
                                              select new SysUserModel
                                              {
													Id = r.Id,
													UserName = r.UserName,
													Password = r.Password,
													TrueName = r.TrueName,
													Card = r.Card,
													MobileNumber = r.MobileNumber,
													PhoneNumber = r.PhoneNumber,
													QQ = r.QQ,
													EmailAddress = r.EmailAddress,
													OtherContact = r.OtherContact,
													Province = r.Province,
													City = r.City,
													Village = r.Village,
													Address = r.Address,
													State = r.State,
													CreateTime = r.CreateTime,
													CreatePerson = r.CreatePerson,
													Sex = r.Sex,
													Birthday = r.Birthday,
													JoinDate = r.JoinDate,
													Marital = r.Marital,
													Political = r.Political,
													Nationality = r.Nationality,
													Native = r.Native,
													School = r.School,
													Professional = r.Professional,
													Degree = r.Degree,
													DepId = r.DepId,
													PosId = r.PosId,
													Expertise = r.Expertise,
													JobState = r.JobState,
													Photo = r.Photo,
													Attach = r.Attach,
													Lead = r.Lead,
													LeadName = r.LeadName,
													IsSelLead = r.IsSelLead,
													IsReportCalendar = r.IsReportCalendar,
													IsSecretary = r.IsSecretary,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, SysUserModel model)
        {
            try
            {
                SysUser entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new SysUser();
               				entity.Id = model.Id;
				entity.UserName = model.UserName;
				entity.Password = model.Password;
				entity.TrueName = model.TrueName;
				entity.Card = model.Card;
				entity.MobileNumber = model.MobileNumber;
				entity.PhoneNumber = model.PhoneNumber;
				entity.QQ = model.QQ;
				entity.EmailAddress = model.EmailAddress;
				entity.OtherContact = model.OtherContact;
				entity.Province = model.Province;
				entity.City = model.City;
				entity.Village = model.Village;
				entity.Address = model.Address;
				entity.State = model.State;
				entity.CreateTime = model.CreateTime;
				entity.CreatePerson = model.CreatePerson;
				entity.Sex = model.Sex;
				entity.Birthday = model.Birthday;
				entity.JoinDate = model.JoinDate;
				entity.Marital = model.Marital;
				entity.Political = model.Political;
				entity.Nationality = model.Nationality;
				entity.Native = model.Native;
				entity.School = model.School;
				entity.Professional = model.Professional;
				entity.Degree = model.Degree;
				entity.DepId = model.DepId;
				entity.PosId = model.PosId;
				entity.Expertise = model.Expertise;
				entity.JobState = model.JobState;
				entity.Photo = model.Photo;
				entity.Attach = model.Attach;
				entity.Lead = model.Lead;
				entity.LeadName = model.LeadName;
				entity.IsSelLead = model.IsSelLead;
				entity.IsReportCalendar = model.IsReportCalendar;
				entity.IsSecretary = model.IsSecretary;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, SysUserModel model)
        {
            try
            {
                SysUser entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.Id = model.Id;
				entity.UserName = model.UserName;
				entity.Password = model.Password;
				entity.TrueName = model.TrueName;
				entity.Card = model.Card;
				entity.MobileNumber = model.MobileNumber;
				entity.PhoneNumber = model.PhoneNumber;
				entity.QQ = model.QQ;
				entity.EmailAddress = model.EmailAddress;
				entity.OtherContact = model.OtherContact;
				entity.Province = model.Province;
				entity.City = model.City;
				entity.Village = model.Village;
				entity.Address = model.Address;
				entity.State = model.State;
				entity.CreateTime = model.CreateTime;
				entity.CreatePerson = model.CreatePerson;
				entity.Sex = model.Sex;
				entity.Birthday = model.Birthday;
				entity.JoinDate = model.JoinDate;
				entity.Marital = model.Marital;
				entity.Political = model.Political;
				entity.Nationality = model.Nationality;
				entity.Native = model.Native;
				entity.School = model.School;
				entity.Professional = model.Professional;
				entity.Degree = model.Degree;
				entity.DepId = model.DepId;
				entity.PosId = model.PosId;
				entity.Expertise = model.Expertise;
				entity.JobState = model.JobState;
				entity.Photo = model.Photo;
				entity.Attach = model.Attach;
				entity.Lead = model.Lead;
				entity.LeadName = model.LeadName;
				entity.IsSelLead = model.IsSelLead;
				entity.IsReportCalendar = model.IsReportCalendar;
				entity.IsSecretary = model.IsSecretary;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual SysUserModel GetById(object id)
        {
            if (IsExists(id))
            {
                SysUser entity = m_Rep.GetById(id);
                SysUserModel model = new SysUserModel();
                              				model.Id = entity.Id;
				model.UserName = entity.UserName;
				model.Password = entity.Password;
				model.TrueName = entity.TrueName;
				model.Card = entity.Card;
				model.MobileNumber = entity.MobileNumber;
				model.PhoneNumber = entity.PhoneNumber;
				model.QQ = entity.QQ;
				model.EmailAddress = entity.EmailAddress;
				model.OtherContact = entity.OtherContact;
				model.Province = entity.Province;
				model.City = entity.City;
				model.Village = entity.Village;
				model.Address = entity.Address;
				model.State = entity.State;
				model.CreateTime = entity.CreateTime;
				model.CreatePerson = entity.CreatePerson;
				model.Sex = entity.Sex;
				model.Birthday = entity.Birthday;
				model.JoinDate = entity.JoinDate;
				model.Marital = entity.Marital;
				model.Political = entity.Political;
				model.Nationality = entity.Nationality;
				model.Native = entity.Native;
				model.School = entity.School;
				model.Professional = entity.Professional;
				model.Degree = entity.Degree;
				model.DepId = entity.DepId;
				model.PosId = entity.PosId;
				model.Expertise = entity.Expertise;
				model.JobState = entity.JobState;
				model.Photo = entity.Photo;
				model.Attach = entity.Attach;
				model.Lead = entity.Lead;
				model.LeadName = entity.LeadName;
				model.IsSelLead = entity.IsSelLead;
				model.IsReportCalendar = entity.IsReportCalendar;
				model.IsSecretary = entity.IsSecretary;
 
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<SysUserModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<SysUserModel>(x => x.UserName, "UserName");
				 excelFile.AddMapping<SysUserModel>(x => x.Password, "Password");
				 excelFile.AddMapping<SysUserModel>(x => x.TrueName, "TrueName");
				 excelFile.AddMapping<SysUserModel>(x => x.Card, "Card");
				 excelFile.AddMapping<SysUserModel>(x => x.MobileNumber, "MobileNumber");
				 excelFile.AddMapping<SysUserModel>(x => x.PhoneNumber, "PhoneNumber");
				 excelFile.AddMapping<SysUserModel>(x => x.QQ, "QQ");
				 excelFile.AddMapping<SysUserModel>(x => x.EmailAddress, "EmailAddress");
				 excelFile.AddMapping<SysUserModel>(x => x.OtherContact, "OtherContact");
				 excelFile.AddMapping<SysUserModel>(x => x.Province, "Province");
				 excelFile.AddMapping<SysUserModel>(x => x.City, "City");
				 excelFile.AddMapping<SysUserModel>(x => x.Village, "Village");
				 excelFile.AddMapping<SysUserModel>(x => x.Address, "Address");
				 excelFile.AddMapping<SysUserModel>(x => x.State, "State");
				 excelFile.AddMapping<SysUserModel>(x => x.CreateTime, "CreateTime");
				 excelFile.AddMapping<SysUserModel>(x => x.CreatePerson, "CreatePerson");
				 excelFile.AddMapping<SysUserModel>(x => x.Sex, "Sex");
				 excelFile.AddMapping<SysUserModel>(x => x.Birthday, "Birthday");
				 excelFile.AddMapping<SysUserModel>(x => x.JoinDate, "JoinDate");
				 excelFile.AddMapping<SysUserModel>(x => x.Marital, "Marital");
				 excelFile.AddMapping<SysUserModel>(x => x.Political, "Political");
				 excelFile.AddMapping<SysUserModel>(x => x.Nationality, "Nationality");
				 excelFile.AddMapping<SysUserModel>(x => x.Native, "Native");
				 excelFile.AddMapping<SysUserModel>(x => x.School, "School");
				 excelFile.AddMapping<SysUserModel>(x => x.Professional, "Professional");
				 excelFile.AddMapping<SysUserModel>(x => x.Degree, "Degree");
				 excelFile.AddMapping<SysUserModel>(x => x.DepId, "DepId");
				 excelFile.AddMapping<SysUserModel>(x => x.PosId, "PosId");
				 excelFile.AddMapping<SysUserModel>(x => x.Expertise, "Expertise");
				 excelFile.AddMapping<SysUserModel>(x => x.JobState, "JobState");
				 excelFile.AddMapping<SysUserModel>(x => x.Photo, "Photo");
				 excelFile.AddMapping<SysUserModel>(x => x.Attach, "Attach");
				 excelFile.AddMapping<SysUserModel>(x => x.Lead, "Lead");
				 excelFile.AddMapping<SysUserModel>(x => x.LeadName, "LeadName");
				 excelFile.AddMapping<SysUserModel>(x => x.IsSelLead, "IsSelLead");
				 excelFile.AddMapping<SysUserModel>(x => x.IsReportCalendar, "IsReportCalendar");
				 excelFile.AddMapping<SysUserModel>(x => x.IsSecretary, "IsSecretary");
 
            //SheetName
            var excelContent = excelFile.Worksheet<SysUserModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new SysUserModel();
						 				  entity.Id = row.Id;
				  entity.UserName = row.UserName;
				  entity.Password = row.Password;
				  entity.TrueName = row.TrueName;
				  entity.Card = row.Card;
				  entity.MobileNumber = row.MobileNumber;
				  entity.PhoneNumber = row.PhoneNumber;
				  entity.QQ = row.QQ;
				  entity.EmailAddress = row.EmailAddress;
				  entity.OtherContact = row.OtherContact;
				  entity.Province = row.Province;
				  entity.City = row.City;
				  entity.Village = row.Village;
				  entity.Address = row.Address;
				  entity.State = row.State;
				  entity.CreateTime = row.CreateTime;
				  entity.CreatePerson = row.CreatePerson;
				  entity.Sex = row.Sex;
				  entity.Birthday = row.Birthday;
				  entity.JoinDate = row.JoinDate;
				  entity.Marital = row.Marital;
				  entity.Political = row.Political;
				  entity.Nationality = row.Nationality;
				  entity.Native = row.Native;
				  entity.School = row.School;
				  entity.Professional = row.Professional;
				  entity.Degree = row.Degree;
				  entity.DepId = row.DepId;
				  entity.PosId = row.PosId;
				  entity.Expertise = row.Expertise;
				  entity.JobState = row.JobState;
				  entity.Photo = row.Photo;
				  entity.Attach = row.Attach;
				  entity.Lead = row.Lead;
				  entity.LeadName = row.LeadName;
				  entity.IsSelLead = row.IsSelLead;
				  entity.IsReportCalendar = row.IsReportCalendar;
				  entity.IsSecretary = row.IsSecretary;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<SysUserModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        SysUser entity = new SysUser();
                       						entity.Id = ResultHelper.NewId;
						entity.UserName = model.UserName;
						entity.Password = model.Password;
						entity.TrueName = model.TrueName;
						entity.Card = model.Card;
						entity.MobileNumber = model.MobileNumber;
						entity.PhoneNumber = model.PhoneNumber;
						entity.QQ = model.QQ;
						entity.EmailAddress = model.EmailAddress;
						entity.OtherContact = model.OtherContact;
						entity.Province = model.Province;
						entity.City = model.City;
						entity.Village = model.Village;
						entity.Address = model.Address;
						entity.State = model.State;
						entity.CreateTime = ResultHelper.NowTime;
						entity.CreatePerson = model.CreatePerson;
						entity.Sex = model.Sex;
						entity.Birthday = model.Birthday;
						entity.JoinDate = model.JoinDate;
						entity.Marital = model.Marital;
						entity.Political = model.Political;
						entity.Nationality = model.Nationality;
						entity.Native = model.Native;
						entity.School = model.School;
						entity.Professional = model.Professional;
						entity.Degree = model.Degree;
						entity.DepId = model.DepId;
						entity.PosId = model.PosId;
						entity.Expertise = model.Expertise;
						entity.JobState = model.JobState;
						entity.Photo = model.Photo;
						entity.Attach = model.Attach;
						entity.Lead = model.Lead;
						entity.LeadName = model.LeadName;
						entity.IsSelLead = model.IsSelLead;
						entity.IsReportCalendar = model.IsReportCalendar;
						entity.IsSecretary = model.IsSecretary;
 
                        db.SysUser.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
